/*******************************************************************************
 *
 * File Name:          GenericAction.java
 *
 * Created:            2007-09-03 20:46:07
 *
 * Original Author:    yu.peng
 *
 *------------------------------------------------------------------------------
 * MODIFICATION HISTORY
 *------------------------------------------------------------------------------
 * WHEN                WHO                 WHY
 * 2007-09-03 20:46:07 yu.peng             new
 *------------------------------------------------------------------------------
 *
 ******************************************************************************/
/*******************/
/* PACKAGE SECTION */
/*******************/
package org.njo.webapp.root.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * 为应用程序的Action提供共通功能.
 * 1,addMessage(),用指定的property名,向request中添加消息.
 * 2,addMessage(),向request中添加消息.
 * 
 *
 * @author yu.peng
 * @version 0.01
 */
public class GenericAction extends Action {

    // --------------------------------------------------------- Public Methods
    public final static String GENERIC_REQUEST_PROPERTY = "generic";
    
    /**
     * 向request中添加消息对象.
     * 
     * @param request HttpServletRequest
     * @param property 属性名
     * @param message ActionMessage
     */
    protected void addMessage(HttpServletRequest request, String property, ActionMessage message) {
        if (message==null) {
            return;
        }
        
        // get any existing messages from the request, or make a new one
        ActionMessages requestMessages = (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
        if (requestMessages == null){
            requestMessages = new ActionMessages();
        }
        // add incoming message
        requestMessages.add(property, message);
        
        // if still empty, just wipe it out from the request
        if (requestMessages.isEmpty()) {
            request.removeAttribute(Globals.MESSAGE_KEY);
            return;
        }

        // Save the messages
        request.setAttribute(Globals.MESSAGE_KEY, requestMessages);
    }
    
    /**
     * 
     * @param request
     * @param message
     */
    protected void addMessage(HttpServletRequest request, ActionMessage message) {
        if (message==null) {
            return;
        }
        
        // get any existing messages from the request, or make a new one
        ActionMessages requestMessages = (ActionMessages) request.getAttribute(Globals.MESSAGE_KEY);
        if (requestMessages == null){
            requestMessages = new ActionMessages();
        }
        // add incoming message
        requestMessages.add(GENERIC_REQUEST_PROPERTY, message);
        
        // if still empty, just wipe it out from the request
        if (requestMessages.isEmpty()) {
            request.removeAttribute(Globals.MESSAGE_KEY);
            return;
        }

        // Save the messages
        request.setAttribute(Globals.MESSAGE_KEY, requestMessages);
    }
    


}
